/**
 * @author Casanova
 */

function $1(id)
{
	return document.getElementById(id);
}

function pageLoad()
{
	generateRandomCode()
}

function generateRandomCode()
{
	var chuoi = "0123456789abcdefghiklmnopqrstuvwxyz";
	var kq = "";
	for(i = 0; i < 8; i++)
	{
		kq += chuoi.charAt(Math.floor(Math.random()*35));
		$1("pic" + (i + 1)).src = "../confirmImages/" + kq.charAt(i) + ".jpg";
	}
	$1("hdCode").value = kq;
	return false;
}

function checkPasswordLength(tbPassword)
{
	var chuoi = tbPassword.value;
	if(chuoi.length < 6)
	{
		$1("tbPasswordStrength").style.background = "red";
		return;
	}
	
	var regexSo = new RegExp('[0-9]');
	var regexChar = new RegExp('[a-z]|[A-Z]');
	if(regexSo.test(chuoi) || regexChar.test(chuoi))
	{
		$1("tbPasswordStrength").style.background = "pink";
		return;
	}
	
	var regexCharNum = new RegExp('[a-z]|[A-Z]|[0-9]');
	if(regexCharNum.test(chuoi))
	{
		$1("tbPasswordStrength").style.background = "blue";
		return;
	}
}

function reset()
{
	$(".error").css('display', 'none');
	$1("tbEmail").value = "";
	$1("tbPassword").value = "";
	$1("tbPasswordStrength").style.background = "white";
	$1("tbConfirmPassword").value = "";
	$1("tbFirstName").value = "";
	$1("tbLastName").value = "";
	$1("tbCharacters").value = "";
	$1("tbPhone").value = "";
	$1('tbUsername').value = "";
}

function commit()
{
	result = true;
	$(".error").css('display', 'none');
	if($("#tbUsername")[0].value == "")
	{
		$("#erUsername").css('display', 'inline');
		result = false;
	}
	else
	{
		var url = '../Ajax/UserProcess.php?type=0&username=' + $("#tbUsername")[0].value;
		$.getJSON(url, function(data) {
			if(data.result == true)
			{
				$("#erUsernameValid").css('display', 'inline');
				result = false;
			}
		});
	}
	if($("#tbEmail")[0].value == "")
	{
		$("#erEmail").css('display', 'inline');
		result = false;
	}
	if($("#tbPassword")[0].value.length < 6)
	{
		$("#erPassword").css('display', 'inline');
		result = false;
	}
	if($("#tbPassword")[0].value != $("#tbConfirmPassword")[0].value)
	{
		$("#erConfirmPassword").css('display', 'inline');
		result = false;
	}
	if($("#tbCharacters")[0].value != $("#hdCode")[0].value)
	{
		$("#erCheckCode").css('display', 'inline');
		result = false;
	}
	if(result == true)
	{
		document.forms[0].submit();
	}
}
